package com.lottery.im.module.chat.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lottery.im.R;
import com.lottery.im.module.tim.model.GroupMemberProfile;
import com.lottery.im.util.transformations.CropCircleTransformation;
import com.tencent.imsdk.TIMGroupMemberRoleType;
import java.util.List;

public class GroupProfileMemberAdapter extends BaseAdapter {

  private Context mContext;
  private LayoutInflater mInflater;//布局装载器对象
  private List<GroupMemberProfile> mProfiles;
  private TIMGroupMemberRoleType mRoleType;

  public GroupProfileMemberAdapter(Context context, List<GroupMemberProfile> profiles,
      TIMGroupMemberRoleType roleType) {
    this.mProfiles = profiles;
    mInflater = LayoutInflater.from(context);
    mContext = context;
    mRoleType = roleType;
  }

  @Override public int getCount() {
    return mProfiles.size();
  }

  @Override public Object getItem(int position) {
    return mProfiles.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if (null == convertView) {
      convertView = mInflater.inflate(R.layout.item_group_profile_member, null);
      holder = new ViewHolder();
      holder.ivProfile = convertView.findViewById(R.id.iv_profile);
      holder.tvName = convertView.findViewById(R.id.tv_name);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    if (mRoleType == TIMGroupMemberRoleType.Admin || mRoleType == TIMGroupMemberRoleType.Owner) {

      if (position == mProfiles.size() - 1) {
        holder.tvName.setText("踢人");
        holder.ivProfile.setImageResource(R.drawable.btn_sccy);
        holder.tvName.setTextColor(ContextCompat.getColor(mContext, R.color.blue_4f));
      } else if (position == mProfiles.size() - 2) {
        holder.tvName.setText("邀请");
        holder.ivProfile.setImageResource(R.drawable.btn_tjhy);
        holder.tvName.setTextColor(ContextCompat.getColor(mContext, R.color.blue_4f));
      } else {
        holder.tvName.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_gray_dark));
        GroupMemberProfile memberProfile = mProfiles.get(position);
        holder.tvName.setText(memberProfile.getNickName());
        Glide.with(mContext)
            .load(memberProfile.getAvatarUrl())
            .apply(new RequestOptions()
                .bitmapTransform(new CropCircleTransformation())
                .placeholder(R.drawable.icon_conversation_de)
                .error(R.drawable.icon_conversation_de))
            .into(holder.ivProfile);
      }
    } else {
      holder.tvName.setTextColor(ContextCompat.getColor(mContext, R.color.text_color_gray_dark));
      GroupMemberProfile memberProfile = mProfiles.get(position);
      holder.tvName.setText(memberProfile.getNickName());
      Glide.with(mContext)
          .load(memberProfile.getAvatarUrl())
          .apply(new RequestOptions()
              .bitmapTransform(new CropCircleTransformation())
              .placeholder(R.drawable.icon_conversation_de)
              .error(R.drawable.icon_conversation_de))
          .into(holder.ivProfile);
    }
    return convertView;
  }

  class ViewHolder {
    ImageView ivProfile;
    TextView tvName;
  }
}
